package com.ruoyi.zhgd.controller;

import java.text.ParseException;
import java.util.List;
import java.util.Arrays;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaIgnore;
import com.ruoyi.zhgd.domain.bo.YLJReportBo;
import com.ruoyi.zhgd.domain.vo.YLJJLVo;
import com.ruoyi.zhgd.domain.vo.NLaboratoryMechanicsYGroupReportVo;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;

import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.zhgd.domain.vo.NLaboratoryMechanicsYGroupVo;
import com.ruoyi.zhgd.domain.bo.NLaboratoryMechanicsYGroupBo;
import com.ruoyi.zhgd.service.INLaboratoryMechanicsYGroupService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 压力机数据主
 *
 * @author zyb
 * @date 2023-09-11
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/zhgd/laboratoryMechanicsYGroup")
public class NLaboratoryMechanicsYGroupController extends BaseController {

    private final INLaboratoryMechanicsYGroupService iNLaboratoryMechanicsYGroupService;

    /**
     * 查询压力机数据主列表
     */
    @SaCheckLogin
    @GetMapping("/list")
    public TableDataInfo<NLaboratoryMechanicsYGroupVo> list(NLaboratoryMechanicsYGroupBo bo, PageQuery pageQuery) {
        return iNLaboratoryMechanicsYGroupService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出压力机数据主列表
     */
    @SaCheckLogin
    @Log(title = "压力机数据主", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(NLaboratoryMechanicsYGroupBo bo, HttpServletResponse response) {
        List<NLaboratoryMechanicsYGroupVo> list = iNLaboratoryMechanicsYGroupService.queryList(bo);
        ExcelUtil.exportExcel(list, "压力机数据主", NLaboratoryMechanicsYGroupVo.class, response);
    }

    /**
     * 获取压力机数据主详细信息
     *
     * @param id 主键
     */
    @SaCheckLogin
    @GetMapping("/{id}")
    public R<NLaboratoryMechanicsYGroupVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iNLaboratoryMechanicsYGroupService.queryById(id));
    }

    /**
     * 新增压力机数据主
     */
    @SaCheckLogin
    @Log(title = "压力机数据主", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody NLaboratoryMechanicsYGroupBo bo) {
        return toAjax(iNLaboratoryMechanicsYGroupService.insertByBo(bo));
    }

    /**
     * 修改压力机数据主
     */
    @SaCheckLogin
    @Log(title = "压力机数据主", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody NLaboratoryMechanicsYGroupBo bo) {
        return toAjax(iNLaboratoryMechanicsYGroupService.updateByBo(bo));
    }

    /**
     * 批量新增或者修改 压力机数据主
     */
    @SaIgnore
    @RepeatSubmit()
    @PostMapping("/insertOrUpdateBatchByBoList")
    public R<Void> insertOrUpdateBatch(@Validated(AddGroup.class) @RequestBody YLJReportBo reportBo) {
        return toAjax(iNLaboratoryMechanicsYGroupService.insertOrUpdateBatchByBoList(reportBo));
    }

    /**
     * 删除压力机数据主
     *
     * @param ids 主键串
     */
    @SaCheckLogin
    @Log(title = "压力机数据主", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iNLaboratoryMechanicsYGroupService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * 压力机设备信息列表
     */
    @SaCheckLogin
    @GetMapping("/page")
    public TableDataInfo<NLaboratoryMechanicsYGroupVo> page(NLaboratoryMechanicsYGroupBo bo, PageQuery pageQuery) {
        return iNLaboratoryMechanicsYGroupService.page(bo, pageQuery);
    }

    /**
    * 验证采集的数据是否合格
    * @author cj
    * @date 2023/11/14 13:57
    * @return
    **/
    @SaIgnore
    @GetMapping("/verifyEligible")
    public R verifyEligible(@RequestParam Integer id) {
        ScheduledExecutorService executor = Executors.newScheduledThreadPool(10);
        executor.schedule(() -> iNLaboratoryMechanicsYGroupService.verifyEligible(id), 120, TimeUnit.SECONDS);
        return R.ok();
    }

    /**
     * 验证采集的数据是否合格
     * @author cj
     * @date 2023/11/14 13:57
     * @return
     **/
    @SaIgnore
    @GetMapping("/verifyEligiblesyn")
    public R verifyEligiblesyn(@RequestParam Integer id) {
        iNLaboratoryMechanicsYGroupService.verifyEligible(id);
        return R.ok();
    }

    /**
     * 压力机报表数据查询-详细列表部分
     */
    @SaIgnore
    @GetMapping("/reportList")
    public R<List<NLaboratoryMechanicsYGroupReportVo>> report(@RequestParam("sybh") String sybh,@RequestParam("id") Integer id) throws ParseException {
        return iNLaboratoryMechanicsYGroupService.reportList(sybh,id);
    }

    /**
     * 压力机报表数据查询-主信息
     */
    @SaIgnore
    @GetMapping("/reportBs")
    public R<List<YLJJLVo>> reportBs(@RequestParam("sybh") String sybh,@RequestParam("id") Integer id) throws ParseException {
        return iNLaboratoryMechanicsYGroupService.reportBs(sybh,id);
    }



}
